flyui.namespace("flyui.apps.imagemashup2");

/**
 * @class
 * @constructor
 */
flyui.apps.imagemashup2.Application = function() {
	
	var genefinder = null;
	
	var flyted = null;
	
	var bdgp = null;
	
	var deferredSelection = null;
	
	var that = this;
	
	// called by this.init
	
	this.initGenefinder = function() {
	    var _context = "initGenefinder";
	    try {
            
            flyui.info("instantiate a service for the genefinder widget", _context);
            var service = new flyui.flybase.Service("../../../data/flybase");
            
            flyui.info("instantiate a renderer for the genefinder widget", _context);
            var renderPane = document.getElementById("genefinderWidget");
    //      var renderer = new CustomGeneFinderRenderer(); // custom renderer
            var renderer = new flyui.genefinder.DefaultRenderer(); // default renderer
            renderer.setCanvas(renderPane);
            
            flyui.info("instantiate a genefinder widget", _context);
            genefinder = new flyui.genefinder.Widget(service, renderer);
    
	    } catch (e) {
            flyui.err("unexpected error: "+e.name+" "+e.message, _context);
            throw e;
	    }
	};
	
	// called by this.init
	
	this.initFlyted = function() {
	    var _context = "initFlyted";
	    try {
            
            // initialise flyted image widget
            flyui.info("instantiate a service for the flyted image widget", _context);
            var service = new flyui.flyted.Service("../../../data/flyted");
            
            flyui.info("instantiate a renderer for the flyted image widget", _context);
            var renderPane = document.getElementById("flytedImageWidget");
    //      var renderer = new CustomFlytedRenderer();
            var renderer = new flyui.flyted.ImageWidget.DefaultRenderer();
            renderer.setCanvas(renderPane);
            
            flyui.info("instantiate a flyted image widget", _context);
            flyted = new flyui.flyted.ImageWidget(service, renderer);
            
	    } catch (e) {
            flyui.err("unexpected error: "+e.name+" "+e.message, _context);
            throw e;
	    }
	};
	
	// called by this.init
	
	this.initBdgp = function() {
	    var _context = "initBdgp";
        try {
            
            // initialise bdgp image widget
            flyui.info("instantiate a service for the bdgp image widget", _context);
            var service = new flyui.bdgp.Service("../../../data/bdgp");
            
            flyui.info("instantiate a renderer for the bdgp image widget", _context);
            var renderPane = document.getElementById("bdgpImageWidget");
    //      var renderer = new CustomBdgpRenderer();
            var renderer = new flyui.bdgp.ImageWidget.DefaultRenderer();
            renderer.setCanvas(renderPane);
            
            flyui.info("instantiate a bdgp image widget", _context);
            bdgp = new flyui.bdgp.ImageWidget(service, renderer);
            
        } catch (e) {
            flyui.err("unexpected error: "+e.name+" "+e.message, _context);
            throw e;
        }	
	};
	
	// called by this.init
	
	this.initState = function() {
	    var _context = "initState";
	    try {
	        
            var url = new String(window.location.href);
            var qi = url.indexOf("#query=");
            var si = url.indexOf(";selection=");
            
            if (qi > 0) {
                var end = (si > 0) ? si : url.length;
                var query = url.substring(qi+7,end);
                flyui.info("found query: "+query, _context);
                genefinder.findGenesByAnyName(query,false);
            }
            
            if (si > 0) {
                var selection = url.substring(si+11,url.length);
                flyui.info("found selection: "+selection, _context);
                deferredSelection = selection;
            }
            
        } catch (e) {
            flyui.err("unexpected error: "+e.name+" "+e.message, _context);
            throw e;
        }   
	};

	this.init = function() {
		var _context = "init";
		try {

            var logReader = new YAHOO.widget.LogReader("logger");
    
            this.initGenefinder();
            
            flyui.info("init flyted widget", _context);
            this.initFlyted();
            
            flyui.info("init bdgp widget", _context);
            this.initBdgp();
    
            // attach gene selection listener to genefinder
            genefinder.subscribe("GENESELECTED", this.onGeneSelected, null);
            genefinder.subscribe("GENESFOUND", this.onGenesFound, null);
    
            // attach form submission listener to form
            flyui.info("hook form submission to widget call", _context);
            YAHOO.util.Event.addListener("queryForm", "submit", this.onFormSubmitted);
            
            flyui.info("application loaded, showing main pane", _context);
            flyui.mvcutils.hide(document.getElementById("loadingPane"));
            flyui.mvcutils.show(document.getElementById("applicationPane"));
            
            this.initState();

		} catch (error) {
		    flyui.err("unexpected error: "+error.name+" "+error.message, _context);
            throw error;
		}
	};
	
	this.onGenesFound = function(type, args) {
	    var _context = "onGenesFound";
	    try {
            flyui.info("application: genes found", _context);
            var genes = args[0];
            if (deferredSelection != null) {
                flyui.info("attempting to apply deferred selection: "+deferredSelection, _context);
                var index = -1;
                for (var i=0; i<genes.length; i++) {
                    if (deferredSelection == genes[i].flybaseID) {
                        index = i;
                    }
                }
                if (index >= 0 && genes.length > 1) {
                    // N.B. only need to call setSelectionIndex if more than one gene
                    // otherwise it is automatically called by genefinder
                    genefinder.setSelectionIndex(index);
                }
                else {
                    flyui.err("failed to apply deferred selection: "+deferredSelection, _context);
                }
                deferredSelection = null;
            }
        } catch (error) {
            flyui.err("unexpected error: "+error.name+" "+error.message, _context);
            throw error;
        }
	}
	
	this.onGeneSelected = function(type, args) {
	    var _context = "onGeneSelected";
	    try {
	        
            var gene = args[0];
            flyui.info("gene selected: "+gene+" "+gene.flybaseID, _context);
            flyted.findImagesByGene(gene);
            bdgp.findImagesByGene(gene);
            flyui.mvcutils.show(document.getElementById("flytedImageWidget"));
            flyui.mvcutils.show(document.getElementById("bdgpImageWidget"));
            that.setSelectionState(gene);

        } catch (error) {
            flyui.err("unexpected error: "+error.name+" "+error.message, _context);
            throw error;
        }
	};	
	
	this.onFormSubmitted = function(event) {
	    var _context = "onFormSubmitted";
	    try {
	        
            flyui.info("form submitted", _context);
            var query = document.getElementById("queryInput").value;
            document.getElementById("queryInput").value = "";
            var caseSensitive = false;
            flyui.info("case sensitive: "+caseSensitive, _context);
            genefinder.findGenesByAnyName(query, caseSensitive);
            flyui.mvcutils.hide(document.getElementById("flytedImageWidget"));
            flyui.mvcutils.hide(document.getElementById("bdgpImageWidget"));
            that.setQueryState(query);

        } catch (error) {
            flyui.err("unexpected error: "+error.name+" "+error.message, _context);
            throw error;
        }
	};
	
	this.setQueryState = function(query) {
		var url = new String(window.location.href);
		var term = url.indexOf("#");
		if (term > 0) {
			url = url.substring(0, term);
		}
		window.location.href = url + "#query=" + query;
	};
	
	this.setSelectionState = function(gene) {
		var url = new String(window.location.href);
		var term = url.indexOf(";selection=");
		if (term > 0) {
			url = url.substring(0, term);
		}
		window.location.href = url + ";selection=" + gene.flybaseID;
	}
	
	this.init();
};

// on dom ready, instantiate application
YAHOO.util.Event.onDOMReady(function() {new flyui.apps.imagemashup2.Application();});